﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ServEvo.Repos
{
    /// <summary>
    /// Description résumée de ServEvoRepos
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // Pour autoriser l'appel de ce service Web depuis un script à l'aide d'ASP.NET AJAX, supprimez les marques de commentaire de la ligne suivante. 
    // [System.Web.Script.Services.ScriptService]
    public class ServEvoRepos : System.Web.Services.WebService {

        public ServEvoRepos () {
            //Supprimez les marques de commentaire dans la ligne suivante si vous utilisez des composants conçus 
            //InitializeComponent(); 
        }

        [WebMethod]
        public string getAllRepos(bool isX64 = false)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("apache" + "::" + _getServiceVersion("apache") + "::" + _getServiceLastVersionURI("apache", isX64) + "::" + _getServiceLastVersionMD5("apache", isX64) + ";");
            sb.Append("mysql" + "::" + _getServiceVersion("mysql") + "::" + _getServiceLastVersionURI("mysql", isX64) + "::" + _getServiceLastVersionMD5("mysql", isX64) + ";");
            sb.Append("php" + "::" + _getServiceVersion("php") + "::" + _getServiceLastVersionURI("php", isX64) + "::" + _getServiceLastVersionMD5("php", isX64) + ";");
            sb.Append("ftp" + "::" + _getServiceVersion("ftp") + "::" + _getServiceLastVersionURI("ftp", isX64) + "::" + _getServiceLastVersionMD5("ftp", isX64) + ";");
            sb.Append("smtp" + "::" + _getServiceVersion("smtp") + "::" + _getServiceLastVersionURI("smtp", isX64) + "::" + _getServiceLastVersionMD5("smtp", isX64));
            return sb.ToString();
        }
        [WebMethod]
        public string getServEvoVersion()
        {
            return _getServiceVersion("core");
        }
        [WebMethod]
        public string getServEvoRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("core", isX64) + "::" + _getServiceLastVersionMD5("core", isX64);
        }
        [WebMethod]
        public string getApacheVersion()
        {
            return _getServiceVersion("apache");
        }
        [WebMethod]
        public string getApacheRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("apache", isX64) + "::" + _getServiceLastVersionMD5("apache", isX64);
        }
        [WebMethod]
        public string getMySQLVersion()
        {
            return _getServiceVersion("mysql");
        }
        [WebMethod]
        public string getMySQLRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("mysql", isX64) + "::" + _getServiceLastVersionMD5("mysql", isX64);
        }
        [WebMethod]
        public string getPHPVersion()
        {
            return _getServiceVersion("php");
        }
        [WebMethod]
        public string getPHPRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("php", isX64) + "::" + _getServiceLastVersionMD5("php", isX64);
        }
        [WebMethod]
        public string getFTPVersion()
        {
            return _getServiceVersion("ftp");
        }
        [WebMethod]
        public string getFTPRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("ftp", isX64) + "::" + _getServiceLastVersionMD5("ftp", isX64);
        }
        [WebMethod]
        public string getSMTPVersion()
        {
            return _getServiceVersion("smtp");
        }
        [WebMethod]
        public string getSMTPRepos(bool isX64 = false)
        {
            return _getServiceLastVersionURI("smtp", isX64) + "::" + _getServiceLastVersionMD5("smtp", isX64);
        }


        private string _getServiceVersion(string sServiceName)
        {
            string sServiceVersion = string.Empty;
            switch (sServiceName)
            {
                case "core":
                    sServiceVersion = ConfigurationManager.AppSettings["CoreLastVersion"] as string;
                    break;
                case "apache":
                    sServiceVersion = ConfigurationManager.AppSettings["ApacheLastVersion"] as string;
                    break;
                case "mysql":
                    sServiceVersion = ConfigurationManager.AppSettings["MySQLLastVersion"] as string;
                    break;
                case "php":
                    sServiceVersion = ConfigurationManager.AppSettings["PHPLastVersion"] as string;
                    break;
                case "ftp":
                    sServiceVersion = ConfigurationManager.AppSettings["FTPLastVersion"] as string;
                    break;
                case "smtp":
                    sServiceVersion = ConfigurationManager.AppSettings["SMTPLastVersion"] as string;
                    break;
                default:
                    break;
            }
            return sServiceVersion;
        }

        private string _getServiceLastVersionURI(string sServiceName, bool isX64)
        {
            string sRequestRawURI = this.Context.Request.RawUrl.ToString();
            string sRequestURI = this.Context.Request.Url.ToString();
            string sServiceLastVersionURI = sRequestURI.Substring(0, sRequestURI.Length - sRequestRawURI.Length);
            if (isX64)
            {
                switch (sServiceName)
                {
                    case "core":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/servevo/" + ConfigurationManager.AppSettings["CoreLastReposx86"] as string;
                        break;
                    case "apache":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/apache/" + ConfigurationManager.AppSettings["ApacheLastReposx64"] as string;
                        break;
                    case "mysql":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/mysql/" + ConfigurationManager.AppSettings["MySQLLastReposx64"] as string;
                        break;
                    case "php":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/php/" + ConfigurationManager.AppSettings["PHPLastReposx86"] as string;
                        break;
                    case "ftp":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/ftp/" + ConfigurationManager.AppSettings["FTPLastReposx86"] as string;
                        break;
                    case "smtp":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/smtp/" + ConfigurationManager.AppSettings["SMTPLastReposx86"] as string;
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (sServiceName)
                {
                    case "core":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/servevo/" + ConfigurationManager.AppSettings["CoreLastReposx86"] as string;
                        break;
                    case "apache":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/apache/" + ConfigurationManager.AppSettings["ApacheLastReposx86"] as string;
                        break;
                    case "mysql":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/mysql/" + ConfigurationManager.AppSettings["MySQLLastReposx86"] as string;
                        break;
                    case "php":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/php/" + ConfigurationManager.AppSettings["PHPLastReposx86"] as string;
                        break;
                    case "ftp":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/ftp/" + ConfigurationManager.AppSettings["FTPLastReposx86"] as string;
                        break;
                    case "smtp":
                        sServiceLastVersionURI += "/ServEvoRepo/repository/smtp/" + ConfigurationManager.AppSettings["SMTPLastReposx86"] as string;
                        break;
                    default:
                        break;
                }
            }
            return sServiceLastVersionURI;
        }

        private string _getServiceLastVersionMD5(string sServiceName, bool isX64)
        {
            string hashType = String.Empty;
            string hashString = String.Empty;
            if (isX64)
            {
                switch (sServiceName)
                {
                    case "core":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["CoreLastReposx86_MD5"] as string;
                        break;
                    case "apache":
                        hashType = "SHA1";
                        hashString = ConfigurationManager.AppSettings["ApacheLastReposx64_SHA1"] as string;
                        break;
                    case "mysql":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["MySQLLastReposx64_MD5"] as string;
                        break;
                    case "php":
                        hashType = "SHA1";
                        hashString = ConfigurationManager.AppSettings["PHPLastReposx86_SHA1"] as string;
                        break;
                    case "ftp":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["FTPLastReposx86_MD5"] as string;
                        break;
                    case "smtp":
                        hashType = "MD5";
                        hashString = "";
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (sServiceName)
                {
                    case "core":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["CoreLastReposx86_MD5"] as string;
                        break;
                    case "apache":
                        hashType = "SHA1";
                        hashString = ConfigurationManager.AppSettings["ApacheLastReposx86_SHA1"] as string;
                        break;
                    case "mysql":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["MySQLLastReposx86_MD5"] as string;
                        break;
                    case "php":
                        hashType = "SHA1";
                        hashString = ConfigurationManager.AppSettings["PHPLastReposx86_SHA1"] as string;
                        break;
                    case "ftp":
                        hashType = "MD5";
                        hashString = ConfigurationManager.AppSettings["FTPLastReposx86_MD5"] as string;
                        break;
                    case "smtp":
                        hashType = "MD5";
                        hashString = "";
                        break;
                    default:
                        break;
                }
            }
            return hashType + "::" + hashString;
        }
    }
}
